package com.gome.security.util.DSA;

import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

/**
 * DSA签名校验
 * 
 * @author tx_hemaojie
 * 
 */
public class DSACoderTest {

	// 公钥
	private static byte[] publicKey;
	// 私钥
	private static byte[] privateKey;

	/**
	 * 初始化密钥
	 * 
	 * @throws Exception
	 */

	public void initKey() throws Exception {

		Map<String, Object> keyMap = DSACoder.initKey();
		publicKey = DSACoder.getPublicKey(keyMap);
		privateKey = DSACoder.getPrivateKey(keyMap);
		System.out.println("公钥: \n" + Base64.encodeBase64String(publicKey));
		System.out.println("私钥: \n" + Base64.encodeBase64String(privateKey));

	}

	public static void main(String str[]) throws Exception {
		String inputstr = "DSA数字签名";
		byte[] data = inputstr.getBytes();
		DSACoderTest dct = new DSACoderTest();
		dct.initKey();
		
		byte[] sign = DSACoder.sign(data, privateKey);
		System.out.println("签名: \n" + Hex.encodeHexString(sign));

		boolean status = DSACoder.verify(data, publicKey, sign);
		System.out.println("签名验证: \n" + status);

	}

}
